<?php
require_once (APPLICATION_DIR."/application/model/Programme.php");
class Programme extends DataObjects_Programme
{	
	function getAdmissionPageByID($pro_id,$type)
	{
	    $this->selectAdd();	    
	    switch ($type)
	    {
	        case PROGRAMME_REQUIREMENT_INDEX:
	            $this->selectAdd("pro_requirement as pro_content");
	            break;
	        
	        case PROGRAMME_PROCEDURE_INDEX:
	            $this->selectAdd("pro_procedure as pro_content");
	            break;
	            
	        case PROGRAMME_APPLYFORM_INDEX:
	            $this->selectAdd("pro_applyform as pro_content");
	            break;
	            
            case PROGRAMME_APPLYONLINE_INDEX:
	            $this->selectAdd("pro_applyonline as pro_content");
	            break;
	            
            default:
                die("Unsual error!");
                return false;
                break;
	    }
	    $s = new Submenu();
	    $s = $s->tableName();
	    $this->selectAdd("s1.menu_title as pro_title, s2.menu_title as partner_title");
	    
	    $pa = new Partner();
	    $pa = $pa->tableName();
	    
	    $this->joinAdd();
	    $this->_join = "join $s s1 on (pro_menu_id = s1.menu_id) join $pa on (pro_partner_id = $pa.partner_id)";
	    
	    $this->whereAdd();
	    $this->whereAdd("pro_id = $pro_id");
		$this->find();
		return $this;
	}
	
	function addProgramme($submenu_id,$partner_id)
	{
	    $pro = new Programme();
	    $pro->pro_menu_id = $submenu_id;
	    $pro->pro_partner_id = $partner_id;
	    $pro->find();
	    if ($pro->fetch())
	    {
	        return "Partner's page of this programme existed. Please see on the above list.";
	    }
	    else 
	    {
    	    $this->pro_menu_id = $submenu_id;
    	    $this->pro_partner_id = $partner_id;
    	    $this->insert();
	    }
	}
	
	function getAdmissionInfo($pro_menu_id,$type,$partner_id)
	{
	    $this->selectAdd();
	    $this->selectAdd("pro_id");
	    switch ($type)
	    {
	        case PROGRAMME_REQUIREMENT_INDEX:
	            $this->selectAdd("pro_requirement as pro_content");
	            break;
	        
	        case PROGRAMME_PROCEDURE_INDEX:
	            $this->selectAdd("pro_procedure as pro_content");
	            break;
	            
	        case PROGRAMME_APPLYFORM_INDEX:
	            $this->selectAdd("pro_applyform as pro_content");
	            break;
	            
            case PROGRAMME_APPLYONLINE_INDEX:
	            $this->selectAdd("pro_applyonline as pro_content");
	            break;
	            
            default:
                die("Unsual error!");
                return false;
                break;
	    }
	    
	    $s = new Submenu();  $s = $s->tableName();
	    $pa = new Partner(); $pa = $pa->tableName();
	    
	    $this->selectAdd("(select menu_title from $s where ($s.menu_id = $pro_menu_id)) as pro_title");
	    $this->selectAdd("(select partner_title from $pa where ($pa.partner_id = $partner_id)) as partner_title");
	    $this->whereAdd();
	    $this->whereAdd("(pro_menu_id = $pro_menu_id) and (pro_partner_id = $partner_id)");
	    $this->find();
	    return $this;
	}
	
	function updateAdmissionInfo($pro_id,$content,$type)
	{
	    $this->pro_id = $pro_id;
	    switch ($type)
	    {
	        case PROGRAMME_REQUIREMENT_INDEX:
	            $this->pro_requirement = $content;
	            break;
	        
	        case PROGRAMME_PROCEDURE_INDEX:
	            $this->pro_procedure = $content;
	            break;
	            
	        case PROGRAMME_APPLYFORM_INDEX:
	            //delete previous file (if any)
	            $pro = new Programme();
	            $pro->pro_id = $pro_id;
	            $pro->selectAdd();
	            $pro->selectAdd("*");
	            $pro->find(); $pro->fetch();
	            if ($pro->pro_applyform)
	            {
	                @unlink($pro->pro_applyform);
	            }
	            
	            $this->pro_applyform = $content;
	            break;
	            
            case PROGRAMME_APPLYONLINE_INDEX:
	            $this->pro_applyonline = $content;
	            break;
	            
            default:
                return false;
                break;
	    }
	    $this->update();
	}
	
	function deleteAdmission($menu_id)
	{
	    $table_name = $this->tableName();
	    $sql = "delete from $table_name where pro_menu_id = $menu_id";
	    $this->query($sql);
	}
		
	function getAllPartnerAdmission()
	{
       $submenu = new Submenu(); $s1 = $submenu->tableName();
       $pa = new Partner();      $pa = $pa->tableName();
       $p = $this->tableName();
       	    
	   $sql = "select $p.pro_id as pro_id, s1.menu_id as pro_menu_id, $pa.partner_id as partner_id, s1.menu_title as pro_title, $pa.partner_title as partner_title,$p.pro_requirement as pro_requirement,$p.pro_procedure as pro_procedure,$p.pro_applyform as pro_applyform
	           from $p
               join $s1 as s1
	               on ($p.pro_menu_id = s1.menu_id)
               join $pa
	               on ($p.pro_partner_id = $pa.partner_id)
                where ($p.pro_partner_id is not NULL)
                order by $p.pro_id";
	   $this->query($sql);
	   return $this;
	}
	
	function getAllAdmissionOfPartner($pro_partner_id)
	{
       $submenu = new Submenu();    $s1 = $submenu->tableName();
       $pa = new Partner();         $pa = $pa->tableName();
       $p = $this->tableName();
	   $sql = "select $p.pro_id as pro_id, s1.menu_id as pro_menu_id, $pa.partner_id as partner_id, s1.menu_title as pro_title, $pa.partner_title as partner_title,$p.pro_requirement as pro_requirement,$p.pro_procedure as pro_procedure ,$p.pro_applyform as pro_applyform
	           from $p
               join $s1 as s1
	               on ($p.pro_menu_id = s1.menu_id)
               join $pa
	               on ($p.pro_partner_id = $pa.partner_id) 
                where ($p.pro_partner_id = $pro_partner_id )
                order by $p.pro_id";
	   $this->query($sql);	   
	   return $this;
	}
	
	function getProgramList()
	{
	    $this->selectAdd();
	    $p = $this->tableName();
	    $submenu = new Submenu();
	    $s = $submenu->tableName();
	    $sql = "select distinct $p.pro_menu_id, $s.menu_title as pro_title
	            from $p,$s
	            where ($p.pro_menu_id = $s.menu_id)";
	    $this->_query($sql);
	    return $this;
	}
	
	function getPartnerList($pro_menu_id)
	{
	    $this->selectAdd();
	    $pa = $pa->tableName();
	    $sql = "select partner_id, partner_title
	            from $pa
	            where (partner_id = $pro_menu_id)";
	    $this->_query($sql);
	    return $this;
	}
	
	function getLastProID()
	{
	    $this->selectAdd();
	    $this->selectAdd("pro_id");
	    $this->orderBy("pro_id DESC");
	    $this->find();
	    if ($this->fetch())
	    {
	        $result = $this->pro_id;
	    }
	    else 
	    {
	        $result = 0;
	    }
	    return $result;
	}
	
	function deleteProgramme($pro_id)
	{
	    $this->pro_id = $pro_id;
	    $this->delete();
	}
}
?>